package com.googlecode.flealancemarket.tapestry.components;

import com.googlecode.flealancemarket.core.tapestry.SpringBean;
import com.googlecode.flealancemarket.metamodel.User;
import com.googlecode.flealancemarket.service.commands.CountUnreadMessages;
import com.googlecode.flealancemarket.service.commands.GetUserByUsername;
import org.apache.tapestry.Asset;
import org.apache.tapestry.annotations.Path;
import org.apache.tapestry.ioc.annotations.Inject;
import com.googlecode.flealancemarket.tapestry.util.Session;
import org.apache.tapestry.annotations.ApplicationState;

public class Border {

        @ApplicationState
        private Session session;

        private int messagesNumber = 0;

        public String getUsername(){
            return session.getUsername();
        }
        public boolean getUserLogged(){
            return !(session.getUserId()==0);
        }

        public boolean getUserIsEmployer() {
            return session.getUserIsEmployer();
        }

        public String getMessage(){
            return session.getMessage();
        }

        public boolean getHasMessage(){
            return session.getHasMessage();
        }
	public Asset getStyles() {
            return styles;
	}

        public Asset getScript() {
            return script;
        }

        public Asset getJquery() {
            return jquery;
        }
    
        public int getMessagesNumber(){
            return messagesNumber;
        }
        public void setMessagesNumber(int mn){
            messagesNumber = mn;
        }

	@Inject
	@Path("context:styles/styles.css")
	private Asset styles;

        @Inject
	@Path("context:script/script.js")
	private Asset script;

        @Inject
	@Path("context:script/jquery.js")
	private Asset jquery;

        @Inject
        @SpringBean("com.googlecode.flealancemarket.service.commands.CountUnreadMessages")
        private CountUnreadMessages countMessages;
        @Inject
        @SpringBean("com.googlecode.flealancemarket.service.commands.GetUserByUsername")
        private GetUserByUsername getUser;

        public boolean getHasMessages(){
            return messagesNumber > 0;
        }
        
        public String getTitle() {
            return "FleaLanceMarket :: fleamarket for/with freelancers";
        }

        Object onPassivate(){
            session.setMessage(null);
            if(getUserLogged()){
                getUser.setUsername(getUsername());
                User user = getUser.execute();
                countMessages.setUser(user);
                int num = countMessages.execute();
                setMessagesNumber(num);
            }
            return null;
        }
}
